﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;

namespace Project_Udrinkwater.PagesAdmin
{
    public partial class GoodsList : System.Web.UI.Page
    {
        ImageTosqlDataContext db = new ImageTosqlDataContext();
       // DataClassesProductDataContext dbp = new DataClassesProductDataContext();
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void detailGrid_DataSelect(object sender, EventArgs e)
        {
            Session["Product_ID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
        }

        protected void ASPxGridView1_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
        {
            if (e.CommandArgs.CommandName == "Cmdedit")
            {
                string GoodID = e.CommandArgs.CommandArgument.ToString();
                Session["GoodID"] = GoodID;
               // Session["Num"] = 1;

                Response.Redirect("Add_And_Edit_Goods.aspx");
            }
            if (e.CommandArgs.CommandName == "Cmddelete")
            {
                string GoodID = e.CommandArgs.CommandArgument.ToString();
                DeleteGoods(GoodID);
            }
        }

        protected void bt_AddGoods_Click(object sender, EventArgs e)
        {
           Response.Redirect("Add_And_Edit_Goods.aspx");
        }
        public void DeleteGoods(string GoodID)
        {
            var p = from b in db.TB_Products
                   where b.Product_ID == GoodID
                    select b;
            db.TB_Products.DeleteAllOnSubmit(p);
            db.SubmitChanges();

            //var q = from a in db.TB_ImagePaths
            //        where a.Image_ID == GoodID
            //        select a;
            //db.TB_ImagePaths.DeleteAllOnSubmit(q);
            //db.SubmitChanges();

            var o = from g in db.TB_Order_Categories
                    where g.Product_ID == GoodID & g.Order_Category_Name == "ขายส่ง"
                    select g;
            db.TB_Order_Categories.DeleteAllOnSubmit(o);
            db.SubmitChanges();

            var s = from t in db.TB_Order_Categories
                    where t.Product_ID == GoodID & t.Order_Category_Name == "ขายปลีก"
                    select t;
            db.TB_Order_Categories.DeleteAllOnSubmit(s);
            db.SubmitChanges();

            ASPxGridView1.DataBind();
        }
       
        }
    
}